package com.white.mysoundrecorder;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Toast;

import java.io.File;

/**
 * Created by yutf on 2017/8/30 0030.
 */

public class RecordEngine
{
    private int state;
    public static final int STATE_IDIL = 0;
    public static final int STATE_RECORDING = 1;

    public static final int CMD_START = 3;
    public static final int CMD_STOP = 4;

    private Context mContext;
    private String path;
    private Intent recordIntent;
    private static RecordEngine instance;

    private RecordEngine(Context ctx, String parentPath)
    {
        mContext = ctx;
        path = parentPath;
        state = STATE_IDIL;
        File f = new File(parentPath);
        if(!f.exists())
        {
            if(!f.mkdirs())
                throw new RuntimeException("文件存储不可用");
        }
        recordIntent = new Intent(mContext, RecordingService.class);
        mContext.bindService(recordIntent, sc, Context.BIND_AUTO_CREATE);
    }

    public static RecordEngine getInstance(Context ctx, String parentPath)
    {
        if(instance == null)
        {
            synchronized (RecordEngine.class)
            {
                if(instance == null)
                {
                    instance = new RecordEngine(ctx, parentPath);
                }
            }
        }
        return instance;
    }

    public static RecordEngine getInstance()
    {
        return instance;
    }

    public void destroy()
    {
        if(sc != null)
        {
            mContext.unbindService(sc);
            mContext.stopService(recordIntent);
        }
    }

    private RecordingService.MyBinder operate;
    private ServiceConnection sc = new ServiceConnection()
    {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service)
        {
            operate = (RecordingService.MyBinder) IMyAidlInterface.Stub.asInterface(service);
            try
            {
                operate.registCallback(new ServiceCallback());
                if(l != null)
                {
                    l.available();
                }
            } catch (RemoteException e)
            {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name)
        {
            state = STATE_IDIL;
        }
    };

    private class ServiceCallback extends IRecordCallback.Stub
    {

        @Override
        public void onSuccess(String path) throws RemoteException
        {
            Toast.makeText(mContext, "保存路径为：" +path, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStartRecording() throws RemoteException
        {
            if(l != null)
            {
                l.onStart();
            }
        }

        @Override
        public void onStop() throws RemoteException
        {
            if(l != null)
            {
                l.onStop();
            }
        }
    }

    public void start()
    {
        if(state == STATE_IDIL)
        {
            state = STATE_RECORDING;
            try
            {
                operate.start(path);
            } catch (RemoteException e)
            {
                e.printStackTrace();
            }
        }
    }

    public void stop()
    {
        if(state == STATE_RECORDING)
        {
            try
            {
                operate.stop();
            } catch (RemoteException e)
            {
                e.printStackTrace();
            }
        }
    }

    public void setUiCallback(UiCallback c)
    {
        l = c;
    }

    private UiCallback l;

    public interface UiCallback
    {
        void available();

        void onStart();

        void onStop();
    }
}
